package com.x.servlet;

import com.x.bean.Achievement;
import com.x.service.user.UserService;
import com.x.service.user.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @author 13925
 */
public class AchievementAddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Achievement achievement = new Achievement();
        String courseNo = req.getParameter("courseNo");
        String courseName = req.getParameter("courseName");
        String score = req.getParameter("score");
        String code = req.getParameter("code");
        achievement.setCourseNo(courseNo);
        achievement.setCourseName(courseName);
        achievement.setScore(score);
        achievement.setCode(code);
        UserService userService = new UserServiceImpl();
        try {
            if (userService.addAchievement(achievement)) {
                resp.sendRedirect(req.getContextPath()+"/jsp/frameAdmin.jsp");
            } else {
                req.setAttribute("error","插入失败");
                req.getRequestDispatcher("achievementAdd.jsp").forward(req, resp);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
